<script lang="ts" setup>
import useLoading from '@/hooks/loading'
import type { FormInstance } from '@arco-design/web-vue/es/form'
import { TitleMap } from '@/config/constants'
import { cloneDeep } from 'lodash'

interface EditProps {
  modelForm: any
  editVisible: boolean
  createData: any
  updateData: any
  title?: string
  dialogStatus: string
  setEditVisible: any
}

const props = withDefaults(defineProps<EditProps>(), {
  editVisible: false,
  modelForm: {
    sex: 1,
    ord: 0,
    status: 1,
    title: '',
    host: '0.0.0.0',
  },
  dialogStatus: 'create',
  createData: () => {},
  updateData: () => {},
  setEditVisible: () => {},
})
const { modelForm } = toRefs(props)
const { loading, setLoading } = useLoading()
const formRef = ref<FormInstance>()
const handleClose = () => {
  props.setEditVisible()
  formRef.value!.resetFields()
}

// 发布
const handleSubmit = () => {
  formRef
    .value!.validate((valid: any) => {
      if (valid) return

      const data = cloneDeep(unref(modelForm))
      !data.host && (data.host = '0.0.0.0')

      data.status = typeof data.status !== 'undefined' ? +data.status : 1
      props.dialogStatus === 'create' ? props.createData(data) : props.updateData(data)

      handleClose()
    })
    .catch((error: any) => {
      console.log('error', error)
    })
    .finally(() => {
      setLoading(false)
    })
}

const visible = ref(false)
watch(
  () => props.editVisible,
  (show) => {
    visible.value = show
  }
)
</script>
<template>
  <a-modal v-model:visible="visible" @ok="handleSubmit" @cancel="handleClose" draggable :width="650" :loading="loading">
    <template #title>{{ TitleMap[dialogStatus] }}</template>
    <a-alert hidden title="文明发言" type="warning" class="mb-5" :closable="true" />
    <a-form ref="formRef" :model="modelForm" auto-label-width>
      <a-form-item field="id" hidden>
        <input type="hidden" name="id" :value="modelForm.id" />
      </a-form-item>

      <a-row :gutter="20">
        <a-col :span="12">
          <a-form-item label="名称" field="name">
            <a-input v-model.trim="modelForm.name" placeholder="请输入名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="标题" field="title">
            <a-input v-model.trim="modelForm.title" placeholder="请输入标题" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="20">
        <a-col :span="12">
          <a-form-item label="主机" field="host">
            <a-input v-model.trim="modelForm.host" placeholder="请输入主机" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="域名" field="domain">
            <a-input v-model.trim="modelForm.domain" placeholder="请输入域名" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="20">
        <a-col :span="12">
          <a-form-item label="排序" field="order">
            <a-input v-model.trim="modelForm.order" placeholder="请输入排序" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" field="status">
            <a-radio-group v-model="modelForm.status">
              <a-radio :value="1">正常</a-radio>
              <a-radio :value="2">禁用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item label="描述" field="description">
        <a-textarea v-model="modelForm.description" :row="4" :max-length="100" allow-clear show-word-limit auto-size />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<style lang="scss"></style>
